package com.xxx.springboot.controller;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxx.springboot.annotation.LoginAuth;
import com.xxx.springboot.constant.Constants;
import com.xxx.springboot.pojo.Result;
import com.xxx.springboot.pojo.UserInfo;
import com.xxx.springboot.pojo.entity.User;
import com.xxx.springboot.service.UserService;

/**
 * login Controller
 */
@Controller
@RequestMapping(value = "/", produces = "application/json;charset=utf-8")
public class LoginController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private UserService userService;
	
	/**
     * 登录
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpSession session,
    		@RequestParam @NotNull String username, @RequestParam @NotNull String password){
    	logger.info("enter method login().");
    	LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    	queryWrapper.eq(User::getUserName, username);
    	User user = userService.getOne(queryWrapper);
    	if(user!=null) {
    		// 登录用户基本信息
    		UserInfo userInfo = new UserInfo();
    		userInfo.setUserId(user.getId());
    		userInfo.setUserName(user.getUserName());
    		userInfo.setMobile(user.getMobile());
    		userInfo.setPositionId(user.getPositionId());
    		userInfo.setRealName(user.getRealName());
    		userInfo.setSex(user.getSex());
    		userInfo.setEmail(user.getEmail());
    		// 登录用户权限点信息
    		Set<String> permissionIds = new HashSet<>();
    		permissionIds.add("pageList");
//    		permissionIds.add("getById");
    		permissionIds.add("getByName");
    		permissionIds.add("add");
    		permissionIds.add("update");
    		permissionIds.add("delete");
    		userInfo.setPermissionIds(permissionIds);
    		// 将登录用户信息放入session中
    		session.setAttribute(Constants.USER_INFO_SESSION_KEY, userInfo);
    		return Result.ok();
    	}
    	return Result.fail();
    }
    
    /**
     * 登出
     */
    @RequestMapping("/logout")
    @ResponseBody
    public String logout(HttpSession session) {
    	logger.info("enter method logout().");
        session.invalidate();
        return Result.ok();
    }
    
    /**
     * 获取session信息
     * @param user
     * @return
     */
    @LoginAuth
    @RequestMapping("/getSessionInfo")
    @ResponseBody
    public String getSessionInfo(@SessionAttribute(name="userInfo") UserInfo userInfo) throws Exception {
    	logger.info("enter method getSessionInfo().");
    	userInfo.setPermissionIds(null);
        return Result.ok(userInfo);
    }
    
}
